package com.example.pixelyear.adapter

import android.view.View
import androidx.recyclerview.widget.RecyclerView
import com.example.pixelyear.Interface.MoveCallBack

/**
 * Package    : com.zjedu.function.Adapter
 * When I wrote this, only The jade emperor and I understood what I was doing, Now God only knows
 * ClassName  : BaseAdapter
 * Description: $Adapter基类
 * Author     : 坐怀灬不乱
 * Date       : 2019/2/25 10:30
 */
abstract class BaseAdapter<L>(var l: MutableList<L> = mutableListOf()) :
    RecyclerView.Adapter<BaseViewHolder>(),
    MoveCallBack {
    var onItemClick: OnItemClick<L>? = null
    var canDrag = false
    var canSwipe = false
    override fun getItemCount(): Int {
        return l.size
    }

    //获取数组
    open fun getList(): MutableList<L> {
        return l
    }

    //更新数组
    open fun updateList(list: MutableList<L>) {
        val previousSize = l.size
        l.clear()
        notifyItemRangeRemoved(0, previousSize)
        l.addAll(list)
        notifyItemRangeInserted(0, list.size)
    }

    open fun add(data: L, index: Int = -1) {
        if (index != -1) {
            l.add(index, data)
            notifyItemInserted(index)
            notifyItemRangeChanged(index, itemCount)
        } else {
            l.add(data)
            notifyItemInserted(l.size)
            notifyItemRangeChanged(l.size, itemCount)
        }
    }

    open fun addAll(data: MutableList<L>) {
        data.forEach { add(it) }
    }

    open fun removeAll() {
        l.clear()
        notifyDataSetChanged()
    }

    //获取一个指定位置的Item
    open fun getItemAtPosition(posi: Int): L {
        return l[posi]
    }

    //更新指定位置Item
    open fun updateItem(position: Int, date: L? = null, payload: String = "") {
        if (position > itemCount)
            return
        if (date != null)
            l[position] = date
        if (payload.isEmpty())
            notifyItemChanged(position)
        else notifyItemChanged(position, payload)
    }

    //移除一个Item 防止崩溃
    open fun remove(position: Int, isNull: (list: MutableList<L>) -> Unit) {
        if (position >= itemCount)
            return
        l.removeAt(position)
        notifyItemRemoved(position)
        // 如果移除的是最后一个，忽略
        if (position != l.size)
            notifyItemRangeChanged(position, l.size - position)
        isNull.invoke(l)
    }

    //批量移除
    open fun remove(list: MutableList<Int>, isNull: (list: MutableList<L>) -> Unit) {
        list.sortWith(Comparator { o1, o2 ->
            return@Comparator Integer.compare(o1, o2)
        })

        for (i in 0 until list.size) {
            var posi = list[i]
            posi -= i
            remove(posi) {}
        }
        isNull.invoke(l)
    }

    open fun isEmpty(): Boolean {
        return l.isEmpty()
    }

    open fun size() = if (l.isNullOrEmpty()) 0 else l.size

    override fun onBindViewHolder(p0: BaseViewHolder, p1: Int) {

    }

    //重写 解决刷新导致图片闪烁问题
    override fun onBindViewHolder(
        holder: BaseViewHolder,
        position: Int,
        payloads: MutableList<Any>
    ) {
        if (l.size > 0 && l.size == itemCount) {
            if (payloads.isNullOrEmpty())
                onBindView(holder.itemView, l[position], position)
            else {
                //这个payloads的长度始终为1 可能是因为刷新单个Item的事，所以先传空
                onItemChangeBindView(holder.itemView, l[position], position, "")
            }
        }

    }

    //强制重写的加载界面方法
    abstract fun onBindView(itemView: View, bean: L, position: Int)

    //不强制重写的刷新界面用的
    open fun onItemChangeBindView(itemView: View, bean: L, position: Int, payload: String) {}

    //这里重写子类就不强制重写
    override fun onMove(before: Int, after: Int) {
    }

    override fun onRemover(position: Int) {
    }

    fun setOnItemClickListener(onItemClick: OnItemClick<L>) {
        this.onItemClick = onItemClick
    }

    interface OnItemClick<L> {
        fun click(bean: L, position: Int) {}
        fun third(bean: L, position: Int) {}
    }
}